import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { useEffect, useState } from "react";
import { StyleSheetManager } from "styled-components";
import { getStyleSheetTarget, resolveThemeWithDiff } from "../../utils/utils";
import { ThemeProvider } from "../../utils/ThemeContext";
import theme1 from "../../theme-data/theme1";
import theme2 from "../../theme-data/theme2";
import useThemeHelper from "./useThemeHelper";
import Animations from "./style/Animations.style";
import { defaultConfig } from "../../utils/config";

var resolveStyleCounts = function resolveStyleCounts() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var result = {};
  Object.keys(data).forEach(function (k) {
    result[k] = data[k].length;
  });
  return result;
};
/**
 * 全局容器
 * @param children
 * @returns {*}
 */


var themeMap = {
  theme1: theme1,
  theme2: theme2
};
export default function Container(_ref) {
  var children = _ref.children,
      _ref$defaultTheme = _ref.defaultTheme,
      defaultTheme = _ref$defaultTheme === void 0 ? "theme1" : _ref$defaultTheme;

  var _useThemeHelper = useThemeHelper(),
      readLocalCurrentTheme = _useThemeHelper.readLocalCurrentTheme; // 主题配置数据


  var _useState = useState({
    current: _objectSpread({}, defaultConfig, {
      schema: defaultTheme
    }),
    prev: {}
  }),
      _useState2 = _slicedToArray(_useState, 2),
      conf = _useState2[0],
      setConf = _useState2[1]; // 当前可供主题组件消费的主题数据


  var _useState3 = useState({}),
      _useState4 = _slicedToArray(_useState3, 2),
      currentTheme = _useState4[0],
      setCurrentTheme = _useState4[1];

  var schema = conf.current.schema; // 得到每个组件的样式个数，供配置中心获取静态资源使用

  var styleCounts = resolveStyleCounts(themeMap[conf.current.schema]);
  var contextValue = {
    schema: schema,
    config: conf,
    originTheme: currentTheme,
    styleCounts: styleCounts,
    setTheme: setConf,
    setDisplayTheme: setCurrentTheme
  };

  useEffect(function () {
    setCurrentTheme(function (theme) {
      return resolveThemeWithDiff(themeMap[conf.current.schema], conf, theme);
    });
  }, [conf]);
  useEffect(function () {
    /*
     * 从本地获取theme数据，如果不存在则创建；
     * */
    readLocalCurrentTheme(function (co) {
      return setConf(co);
    });
  }, []);
  return React.createElement(ThemeProvider, {
    value: contextValue
  }, children, React.createElement(StyleSheetManager, {
    target: getStyleSheetTarget("cpaas-front-ui_other_injection")
  }, React.createElement(React.Fragment, null,  schema !== "theme2" && React.createElement(Animations, {
    theme: currentTheme
  }))));
} // 测试专用container

export function TestUsedContainer(_ref2) {
  var children = _ref2.children,
      GlobalStyle = _ref2.component,
      themeData = _ref2.themeData;

  // 主题配置数据
  var _useState5 = useState({
    current: defaultConfig,
    prev: {}
  }),
      _useState6 = _slicedToArray(_useState5, 2),
      conf = _useState6[0],
      setConf = _useState6[1];

  var _useState7 = useState({}),
      _useState8 = _slicedToArray(_useState7, 2),
      currentTheme = _useState8[0],
      setCurrentTheme = _useState8[1]; // 得到每个组件的样式个数，供配置中心获取静态资源使用


  var styleCounts = resolveStyleCounts(themeData);
  var contextValue = {
    config: conf,
    originTheme: currentTheme,
    styleCounts: styleCounts,
    setTheme: setConf
  }; // 配置发生变化重新解析主题数据

  useEffect(function () {
    setCurrentTheme(function (theme) {
      return resolveThemeWithDiff(themeData, conf, theme);
    });
  }, [conf, themeData]);
  return React.createElement(ThemeProvider, {
    value: contextValue
  }, children, Object.keys(currentTheme).length && React.createElement(GlobalStyle, {
    theme: currentTheme,
    config: conf
  }), React.createElement(Animations, {
    theme: currentTheme
  }));
}
